﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace smallDeimos.Game
{
    public partial class Map : System.Web.UI.Page
    {
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            int bio = (int)UserDefinedFunctions.GetPlayerFloat(UserId, "Biology");
            int sId = (int)UserDefinedFunctions.GetPlayerInt(UserId, "OrginID");
            if (bio == 0) return;

            ImageMap ImageMap1 = new ImageMap();
            ImageMap1.ImageUrl = @"MapCreator.aspx?sid="+sId.ToString()+"&bio="+ bio.ToString();
            List<int[]> systems = UserDefinedFunctions.GetAllSystemsFromBioRange(sId, bio);
            //calculate cell size
            int cellW, cellH;
            cellW = MapDrawer.Width / bio;
            cellH = MapDrawer.Height / bio;
            //Response.Write("cellW/H:" + cellW.ToString() + "/" + cellH.ToString() +"<br>");
            int tY = cellH * bio / 2 - cellH / 2;
            int tX = cellW * bio / 2 - cellW / 2;
            //Response.Write("tx/y" + tX.ToString() + "/" + tY.ToString() + "<br>");

            //create rectangle hotspots
            foreach (int[] t in systems)
            {
                RectangleHotSpot s = new RectangleHotSpot();
                s.Top = cellH * t[1] + tY;
                s.Left = cellW * t[0] + tX;
                s.Right = cellW * (t[0] + 1) + tX;
                s.Bottom = cellH * (t[1] + 1) + tY;
                //Response.Write("TL:(" + s.Top.ToString() + "," + s.Left.ToString() + ")<br/>");
                s.NavigateUrl = "SystemView.aspx?sId=" + UserDefinedFunctions.SystemAtCoordinates(t[0], t[1]).ToString();
                ImageMap1.HotSpots.Add(s);
            }

            this.Page.Controls.Add(ImageMap1);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sId = 0;
            
            GridViewRow row = GridView1.SelectedRow;
            if (row == null)
                return;
            try
            {
                sId = int.Parse(row.Cells[0].Text);
                
                Response.Redirect("SystemView.aspx?sId=" + sId );
            }
            catch (Exception ex)
            {
                if (row == null)
                    throw new Exception("Row null", ex);

                StringBuilder sb = new StringBuilder("_");
                foreach (TableCell c in row.Cells)
                    sb.Append(c.Text + "_");
                throw new Exception(sb.ToString(), ex);
            }
        }
    }
}
